<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="设备id" prop="hardwareInfo">
        <el-input
          v-model="queryParams.hardwareInfo"
          placeholder="请输入hardwareinfo"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="应用" prop="appcode">
        <el-select v-model="queryParams.appcode" placeholder="应用" filterable clearable style="width: 100%">
          <el-option
            v-for="item in productList"
            :key="item.appcode"
            :label="item.name"
            :value="item.appcode" />
        </el-select>
      </el-form-item>
      <el-form-item label="渠道" prop="channel">
        <el-select v-model="queryParams.channel" placeholder="渠道" filterable clearable style="width: 100%">
          <el-option
          v-for="item in dict.type.sys_channel"
          :key="item.value"
          :label="item.label"
          :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="oaid" prop="oaid">
        <el-input
          v-model="queryParams.oaid"
          placeholder="请输入oaid"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="隐私id" prop="privacyId">
        <el-input
          v-model="queryParams.privacyId"
          placeholder="请输入隐私id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="用户id" align="center" prop="uid" /> -->
      <el-table-column label="hardwareInfo" align="center" prop="hardwareInfo" />
      <el-table-column label="oaid" align="center" prop="oaid" />
      <el-table-column label="隐私id" align="center" prop="privacyId" />
      <el-table-column label="应用" align="center" prop="appName"/>
      <!-- <el-table-column label="应用" align="center" prop="appcode">
        <template slot-scope="scope">
          <dict-tag :options="productList" :value="scope.row.appcode"/>
        </template>
      </el-table-column> -->
      <el-table-column label="省" align="center" prop="province" />
      <el-table-column label="市" align="center" prop="city" />
      <el-table-column label="区" align="center" prop="district" />
      <!-- <el-table-column label="状态" align="center" prop="status" /> -->
      <el-table-column label="渠道" align="center" prop="channel" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_channel" :value="scope.row.channel"/>
        </template>
      </el-table-column>
      <!-- <el-table-column label="手机序列号" align="center" prop="imei" /> -->
      <!-- <el-table-column label="mac地址" align="center" prop="mac" /> -->
      <!-- <el-table-column label="mac地址" align="center" prop="androidid" /> -->
      <!-- <el-table-column label="平台code" align="center" prop="planmedia" /> -->
      <!-- <el-table-column label="计划id" align="center" prop="planid" /> -->
      <!-- <el-table-column label="投放计划绑定时间" align="center" prop="planbindtime" /> -->
      <!-- <el-table-column label="手机序列号" align="center" prop="imei2" /> -->
      <!-- <el-table-column label="创建日期" align="center" prop="opdate" /> -->
      <el-table-column label="创建时间" align="center" prop="createtime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createtime) }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="注册日期" align="center" prop="registerdate" /> -->
      <!-- <el-table-column label="最后登录时间" align="center" prop="lastlogintime" > -->
      <!-- <el-table-column label="最后修改时间" align="center" prop="lastmodifytime" /> -->
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:user:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:user:remove']"
          >删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listUser, getUser, delUser, addUser, updateUser, exportUser } from "@/api/business/user/user";
import { getAllAppList } from '@/api/configs/appConfig';

export default {
  name: "User",
  dicts: ['sys_channel'],
  data() {
    return {
      // 日期范围
      dateRange: [new Date(),new Date()],
      productList:[],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        hardwareinfo: null,
        appcode: null,
        province: null,
        city: null,
        district: null,
        status: null,
        channel: null,
        imei: null,
        mac: null,
        oaid: null,
        androidid: null,
        planmedia: null,
        planid: null,
        planbindtime: null,
        imei2: null,
        privacyId: null,
        opdate: null,
        createtime: null,
        registerdate: null,
        lastlogintime: null,
        lastmodifytime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        appcode: [
          { required: true, message: "应用代码不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getAppList();
    this.getList();
  },
  methods: {
    formatDate(date) {
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, '0');
      const day = String(date.getDate()).padStart(2, '0');
      return `${year}-${month}-${day}`;
    },
    //查询应用列表
    getAppList(){
      getAllAppList().then(response => {
        this.productList = response.data.map(item => {
          return {
            id: item.id,
            appcode: item.appcode,
            name: item.appname,
            value: item.apppkg
          }
        })
      })
    },
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      let date = [];
      if(this.dateRange){
        date[0] = this.formatDate(this.dateRange[0]);
        date[1] = this.formatDate(this.dateRange[1]);
      }
      listUser(this.addDateRange(this.queryParams, date)).then(response => {
        this.userList = response.rows;
        this.userList.forEach(user=>{
          this.productList.forEach(product=>{
            if(user.appcode===product.appcode){
              user.appName = product.name;
            }
          })
        })
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        uid: null,
        hardwareinfo: null,
        appcode: null,
        province: null,
        city: null,
        district: null,
        status: "0",
        channel: null,
        imei: null,
        mac: null,
        oaid: null,
        androidid: null,
        planmedia: null,
        planid: null,
        planbindtime: null,
        imei2: null,
        privacyId: null,
        opdate: null,
        createtime: null,
        registerdate: null,
        lastlogintime: null,
        lastmodifytime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [new Date(),new Date()];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.uid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const uid = row.uid || this.ids
      getUser(uid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.uid != null) {
            updateUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const uids = row.uid || this.ids;
      this.$modal.confirm('是否确认删除用户编号为"' + uids + '"的数据项？').then(function() {
        return delUser(uids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有用户数据项？').then(() => {
        this.exportLoading = true;
        return exportUser(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
